// Fill out your copyright notice in the Description page of Project Settings.


#include "STUMGWindowsLibrary.h"
#include "Blueprint/UserWidget.h"
#include "GameFramework/PlayerController.h"
#include "MediaTexture.h"
#include "WindowUMGmgrActor.h"
#include "Slate.h"
#include "Framework/Docking/TabManager.h"
#include "Widgets/Layout/SScaleBox.h"

bool USTUMGWindowsLibrary::IsInEditor() 
{
#if WITH_EDITOR
	return true;
#else
	return false;
#endif
}

UUserWidget* USTUMGWindowsLibrary::BuildNewUMGAndWindow(
	APlayerController* playerController, TSubclassOf<UUserWidget> umgClass
	, FVector2D windowSize, FVector2D windowPos, AWindowUMGmgrActor*& mgrActor
)
{

	//if (!playerController) {
	//	return nullptr;
	//}

	//

	//mgrActor = playerController->GetWorld()->SpawnActor<AWindowUMGmgrActor>(AWindowUMGmgrActor::StaticClass());

	//return mgrActor->InitWindowAndUMG( windowSize, windowPos);
	return nullptr;
	
}



bool USTUMGWindowsLibrary::BuildNewViewdWindow
(
	UObject* WorldContextObject, FVector2D windowSize, FVector2D windowPos, AWindowUMGmgrActor*& mgrActor
) {
	mgrActor = nullptr;
	if (!IsValid(WorldContextObject))
		return false;

	if (GEngine && WorldContextObject)
	{
		UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
		if (!World)
			return false;
		mgrActor = World->SpawnActor<AWindowUMGmgrActor>();
		return mgrActor->InitWindowAndUMG(windowSize, windowPos);

	}
	return false;
}


bool USTUMGWindowsLibrary::SetGameWindow(APlayerController* playerController, FVector2D point, FVector2D size)
{
	if (!playerController) {
		return false;
	}
	TSharedPtr<SWindow> minaWindow = playerController->GetWorld()->GetGameViewport()->GetWindow();
	if (minaWindow.IsValid()) 
	{
		minaWindow->Resize(size);
		minaWindow->MoveWindowTo(point);
		return true;
	}
	return false;
}


bool USTUMGWindowsLibrary::SetGameWindowMode(APlayerController* playerController, ESTwindowMode wm)
{
	if (!playerController) {
		return false;
	}
	TSharedPtr<SWindow> minaWindow = playerController->GetWorld()->GetGameViewport()->GetWindow();
	if (minaWindow.IsValid())
	{
		minaWindow->SetWindowMode(USTWindowInfo::GetWindowMode(wm));
		return true;
	}
	return false;
}


bool  USTUMGWindowsLibrary::GetGameWindowInfo(APlayerController* playerController
	, FVector2D& point, FVector2D& size, ESTwindowMode& wm) 
{

	if (!playerController) {
		return false;
	}
	TSharedPtr<SWindow> minaWindow = playerController->GetWorld()->GetGameViewport()->GetWindow();
	if (minaWindow.IsValid())
	{
		size = minaWindow->GetViewportSize();
		point = minaWindow->GetPositionInScreen();
		wm = USTWindowInfo::GetStWindowMode(minaWindow->GetWindowMode());
		return true;
	}
	return false;

}


	

UMediaTexture* USTUMGWindowsLibrary::BuildNewMediaTexture(UObject* WorldContextObject)
{
	if (!WorldContextObject)return nullptr;

	
	UMediaTexture* MediaTexture = NewObject<UMediaTexture>(WorldContextObject, UMediaTexture::StaticClass());

	if (MediaTexture != nullptr)
	{
		MediaTexture->UpdateResource();
		return MediaTexture;
	}
	return nullptr;
}

#include "STTabTest.h"



UUserWidget* USTUMGWindowsLibrary::BuildNewUMGTab
(
	APlayerController* playerController, TSubclassOf<UUserWidget> umgClass
	, FVector2D windowSize, FVector2D windowPos, AWindowUMGmgrActor*& mgrActor
)
{

	if (!playerController)
		return nullptr;




	mgrActor = nullptr;




	//TSharedRef<SWindow> tempWindow = SNew(SWindow)
	//	.AutoCenter(EAutoCenter::PrimaryWorkArea)
	//	.Title(FText::FromString("STtest"))
	//	.IsInitiallyMaximized(false)
	//	.ScreenPosition(windowPos)
	//	.ClientSize(windowSize)
	//	.CreateTitleBar(true)
	//	.SizingRule(ESizingRule::UserSized)
	//	.SupportsMaximize(true)
	//	.SupportsMinimize(false);


	//FSlateApplication::Get().AddWindow(tempWindow, true);

	TSharedPtr<FSTTabTestToolkit> tabTestToolkit = MakeShared<FSTTabTestToolkit>();
	tabTestToolkit->CreateEditorLayout(nullptr, windowSize, windowPos);



	//
	//TSharedRef<FGlobalTabmanager> globalTabmanager =FGlobalTabmanager::Get();

	//globalTabmanager->RegisterTabSpawner("TestSuite1", FOnSpawnTab::CreateRaw(tabTestToolkit.Get(),&FSTTabTestToolkit::OnSpawnTab_Viewport));


	//TSharedPtr<SDockTab>  table = globalTabmanager ->GetOwnerTab();

	//SDockTab* ss = table.Get();

	//TSharedRef<FTabManager::FLayout> Layout = FTabManager::NewLayout("SlateTestSuite_Layout")
	//	->AddArea
	//	(
	//		FTabManager::NewArea(windowSize.X, windowSize.Y)
	//		->SetWindow(FVector2D(windowPos.X, windowPos.Y), false)
	//		->Split
	//		(
	//			FTabManager::NewStack()
	//			//->AddTab("TestSuite2", ETabState::OpenedTab)
	//			->AddTab("TestSuite1", ETabState::OpenedTab)
	//			//->AddTab("RenderTransformManipulator", ETabState::OpenedTab)
	//			//->AddTab("WidgetGalleryTab", ETabState::OpenedTab)
	//			//->SetForegroundTab(FName("TestSuite1"))
	//		)
	//	);

	//	//->AddArea
	//	//(
	//	//	// This area will get a 400x600 window at 10,10
	//	//	FTabManager::NewArea(400, 600)
	//	//	->SetWindow(FVector2D(10, 10), false)
	//	//	//->Split
	//	//	//(
	//	//	//	// The area contains a single tab with the widget reflector.
	//	//	//	FTabManager::NewStack()->AddTab("WidgetReflector", ETabState::OpenedTab)
	//	//	//)
	//	//);

	//TSharedPtr<SWindow> rootWindow = GEngine->GameViewport->GetWindow();

	//FGlobalTabmanager::Get()->RestoreFrom(Layout, rootWindow);

	//FGlobalTabmanager::Get()->spaw()

	//TSharedPtr<SScaleBox> scaleBox;
	//SAssignNew(scaleBox, SScaleBox)
	//	.Stretch(EStretch::Fill)
	//	.StretchDirection(EStretchDirection::Both);

	//tempWindow->SetContent(scaleBox.ToSharedRef());

	//tempWindow->Resize(windowSize);
	//tempWindow->MoveWindowTo(windowPos);

	//FGlobalTabmanager::Get()->SetRootWindow(tempWindow);
	//FSlateNotificationManager::Get().SetRootWindow(RootWindow



	//TSharedRef<FSTTabTestToolkit>  tableTemp =  MakeShared<FSTTabTestToolkit>();
	//
	//UWorld* world = playerController->GetWorld();


	////mgrActor = playerController->GetWorld()->SpawnActor<AWindowUMGmgrActor>(AWindowUMGmgrActor::StaticClass());


	//FGlobalTabmanager::Get()->RegisterTabSpawner("ST_Tab",
	//	FOnSpawnTab::CreateSP(&tableTemp.Get(), &FSTTabTestToolkit::OnSpawnTab_Viewport));

	//TSharedRef<FTabManager::FLayout> StandaloneDefaultLayout = FTabManager::NewLayout(TEXT("Stont Test"))
	//	->AddArea
	//	(
	//		FTabManager::NewPrimaryArea()->SetOrientation(Orient_Vertical)
	//		->Split
	//		(
	//			FTabManager::NewStack()
	//			->SetSizeCoefficient(0.1f)
	//			->AddTab(TEXT("new stone table"), ETabState::OpenedTab)
	//			->SetHideTabWell(true)
	//		)
	//	);


	return nullptr;
}

